﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace RRD.DAL
{
    public enum DatabaseType
    {
        Main,
        RFID
    };

    public class Database
    {
        string connectionStringMain = ConfigurationManager.ConnectionStrings["SQLServerMain"].ToString();
        string connectionStringRFID = ConfigurationManager.ConnectionStrings["SQLServerRFID"].ToString();
        Controller.Logger logger = new Controller.Logger();
        /// <summary>
        /// Metoda zapisująca numer trolley'a i cięzarówki do bazy danych
        /// </summary>
        /// <param name="trolleyNumber">numer trolley'a</param>
        /// <param name="truckNumber">numer cięzarówki</param>
        public void WriteTrolley(string trolleyNumber, string truckNumber)
        {
            using (var connection = new SqlConnection(connectionStringMain))
            {
                var command = new SqlCommand();
                command.Connection = connection;
                command.CommandText = "SalesByCategory";
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter parameterTrolleyNumber = new SqlParameter();
                parameterTrolleyNumber.ParameterName = "@trolleyNumber";
                parameterTrolleyNumber.SqlDbType = SqlDbType.NVarChar;
                parameterTrolleyNumber.Direction = ParameterDirection.Input;
                parameterTrolleyNumber.Value = trolleyNumber;

                SqlParameter parameterTruckNumber = new SqlParameter();
                parameterTruckNumber.ParameterName = "@trolleyNumber";
                parameterTruckNumber.SqlDbType = SqlDbType.NVarChar;
                parameterTruckNumber.Direction = ParameterDirection.Input;
                parameterTruckNumber.Value = truckNumber;
                                
                command.Parameters.Add(parameterTrolleyNumber);
                command.Parameters.Add(parameterTruckNumber);
                
                connection.Open();
                using (var dataReader = command.ExecuteReader())
                {
                    try
                    {
                        while (dataReader.Read())
                        {

                        }
                    }
                    finally
                    {
                        dataReader.Close();
                    }
                }
                connection.Close();
            }
        }

        internal bool ConfirmTrolleyNumber(string trolleyNumber, string truckNumber)
        {
            string query = "";
            bool value = false;
            using (var connection = new SqlConnection(connectionStringMain))
            {
                var command = new SqlCommand(query, connection);
                connection.Open();
                using (var dataReader = command.ExecuteReader())
                {
                    try
                    {
                        if(dataReader.Read())
                            if (dataReader[0].ToString().Contains("true"))
                                value = true;
                    }
                    finally
                    {
                        dataReader.Close();
                    }
                }
                connection.Close();
            }
            return value;
        }

        internal void InsertTrolley(string trolleyNumber, string truckNumber)
        {
            throw new NotImplementedException();
        }

        public bool IsDBConnected(DatabaseType type)
        {
            bool value = false;
            var connectionString = string.Empty;

            switch (type)
            {
                case DatabaseType.Main:
                    connectionString = connectionStringMain;
                    break;
                case DatabaseType.RFID:
                    connectionString = connectionStringRFID;
                    break;
            }
            try
            {
                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    if (connection.State == ConnectionState.Open)
                    {
                        value = true;
                    }
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                logger.WriteLog(ex.Message);
            }
            return value;
        }
    }
}
